package com.bc.web.controller.admin;

import com.bc.web.model.Org;
import com.bc.web.model.OrgRole;
import com.bc.web.model.OrgUser;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSMD5;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class OrgUserController extends FSController{
	
	public void search(){
		Page<OrgUser> items = OrgUser.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("org/orgUserList.html");
	}
	
	public void addPage(){
		setAttr("roleList", OrgRole.dao.selectAll());
		render("org/orgUserAdd.html");
	}
	
	public void updatePage(){
		OrgUser orgUser = OrgUser.dao.findById(getPara("id"));
		setAttr("org", Org.dao.selectByOrgCode(orgUser.getOrgCode()));
		setAttr("roleList", OrgRole.dao.selectAll());
		setAttr("item", orgUser);
		render("org/orgUserUpdate.html");
	}
	
	@Before(Tx.class)
	public void add(){
		OrgUser orgUser = getModel(OrgUser.class,"item",true);
		if(OrgUser.dao.selectByUserName(orgUser.getUserName()) != null){
			renderFail("用户名已存在");
		}else{
			orgUser.setUpdateTime(FSDate.getDateTime());
			orgUser.setPassword(FSMD5.codeFor(orgUser.getPassword()));
			orgUser.setPayPassword(orgUser.getPassword());
			orgUser.setEnable(true);
			orgUser.setIsAdmin(true);
			orgUser.save();
			renderSuccess();
		}
	}
	
	public void update(){
		OrgUser orgUser = getModel(OrgUser.class,"item",true);
		String newPassword = getPara("newPassword");
		if(StrKit.notBlank(newPassword)){
			orgUser.setPassword(FSMD5.codeFor(newPassword));
		}
		orgUser.setUpdateTime(FSDate.getDateTime());
		orgUser.update();
		renderSuccess();
	}
	
	@Before(Tx.class)
	public void delete(){
		Integer id = getParaToInt("id");
		OrgUser.dao.deleteById(id);
		renderSuccess(false);
	}
	
}
